{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Error Handling \n",
    "\n",
    "Errors are bond to happen when we are writing a big peice of code, \n",
    "\n",
    "and when some unexcpeted event occurs, like internet not there in ur API\n",
    "\n",
    "to handle these things propeerly we use Error Handling !!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI We are working 1\n",
      "There was an error \n",
      "Hi we are working 2\n"
     ]
    }
   ],
   "source": [
    "print(\"HI We are working 1\") #1\n",
    "try:\n",
    "    a = 5/0 #2\n",
    "except:\n",
    "    print(\"There was an error \")\n",
    "print(\"Hi we are working 2\") #3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "we are in try block\n",
      "Hi everyone welcome to day 6 of FCS \n",
      "There was an error -  division by zero\n",
      "We are in Finally block\n"
     ]
    }
   ],
   "source": [
    "try: # We are trying a peice of code and lets see if it break or not\n",
    "    print(\"we are in try block\")\n",
    "    file = open(\"fcs.txt\",'r')\n",
    "    statement = file.read()\n",
    "    #file.write(\"hi\")\n",
    "    print(statement)\n",
    "    \n",
    "    a = 5/0\n",
    "    \n",
    "except Exception as e: # Catch the error and print something to conclude \n",
    "    print(\"There was an error - \", e)\n",
    "    \n",
    "finally: # Finally is going to run what ever may happen \n",
    "    print(\"We are in Finally block\")\n",
    "    file.close()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "we are in try block\n",
      "Hi everyone welcome to day 6 of FCS \n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-17-d88932c1060c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstatement\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "print(\"we are in try block\")\n",
    "file = open(\"fcs.txt\",'r')\n",
    "statement = file.read()\n",
    "    #file.write(\"hi\")\n",
    "print(statement)\n",
    "\n",
    "a = 5/0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There was an error -  HI There we have manually kept this error to occour\n",
      "We are in Finally block\n"
     ]
    }
   ],
   "source": [
    "try: # We are trying a peice of code and lets see if it break or not\n",
    "    raise NameError(\"HI There we have manually kept this error to occour\")\n",
    "    \n",
    "except Exception as e: # Catch the error and print something to conclude \n",
    "    print(\"There was an error - \", e)\n",
    "    \n",
    "finally: # Finally is going to run what ever may happen \n",
    "    print(\"We are in Finally block\")\n",
    "    file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OK\n",
      "We are in Finally block\n"
     ]
    }
   ],
   "source": [
    "try: # We are trying a peice of code and lets see if it break or not\n",
    "    googleFormVer = 4\n",
    "    if googleFormVer == 4:\n",
    "        print(\"OK\")\n",
    "    else:\n",
    "        raise NameError(\"HI There we have to update the Google Form to Version 4\")\n",
    "    \n",
    "except Exception as e: # Catch the error and print something to conclude \n",
    "    print(\"There was an error - \", e)\n",
    "    \n",
    "finally: # Finally is going to run what ever may happen \n",
    "    print(\"We are in Finally block\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Try \n",
    "#.  - Raise\n",
    "# Except \n",
    "#. - Exception\n",
    "#finally "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing ??\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# When u code a peice of big program, u want many things as like is \n",
    "# this code gonna work excatly the way it has to  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Linting ?\n",
    "# Linting is used to find out is ur code industry standards yes or no \n",
    "# PyLint - Install ?\n",
    "# pip install pylint\n",
    "\n",
    "# unit testing ?\n",
    "# To test a particualr module is working and giving in the desired yes or no\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " # PyLint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pylint in /opt/anaconda3/lib/python3.7/site-packages (2.4.2)\n",
      "Requirement already satisfied: mccabe<0.7,>=0.6 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (0.6.1)\n",
      "Requirement already satisfied: isort<5,>=4.2.5 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (4.3.21)\n",
      "Requirement already satisfied: astroid<2.4,>=2.3.0 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (2.3.1)\n",
      "Requirement already satisfied: wrapt==1.11.* in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.11.2)\n",
      "Requirement already satisfied: typed-ast<1.5,>=1.4.0; implementation_name == \"cpython\" and python_version < \"3.8\" in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.1)\n",
      "Requirement already satisfied: six==1.12 in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.12.0)\n",
      "Requirement already satisfied: lazy-object-proxy==1.4.* in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.2)\n",
      "\u001b[33mWARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.\n",
      "You should consider upgrading via the '/opt/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install pylint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting some.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile some.py\n",
    "'''\n",
    "this is the docstring for some.py\n",
    "'''\n",
    "A = 10\n",
    "B = 15\n",
    "C = A + B\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "----------------------------------------------------------------------\r\n",
      "Your code has been rated at 10.00/10 (previous run: -15.00/10, +25.00)\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "! pylint some.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "factorial of a number = 10\n",
    "\n",
    "10 * 9 * 8* 7 * 6 * 5 * 4 * 3 * 2 * 1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting factorial.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile factorial.py\n",
    "\"\"\"\n",
    "This is a Python program for gettting factorial Numbers\n",
    "\"\"\"\n",
    "NUM = 10\n",
    "def factorial_function(number):\n",
    "    '''\n",
    "    This is a function for factorial\n",
    "    '''\n",
    "    fact = 1\n",
    "    for i in range(1, number+1):\n",
    "        fact = fact * i\n",
    "    return fact\n",
    "\n",
    "FCT = factorial_function(NUM)\n",
    "print(FCT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "-------------------------------------------------------------------\r\n",
      "Your code has been rated at 10.00/10 (previous run: 8.75/10, +1.25)\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "! pylint factorial.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3628800\r\n"
     ]
    }
   ],
   "source": [
    "! python factorial.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unit Testing - \n",
    "\n",
    "Testing of modules working properly yes or no"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "##UnitTesting \n",
    "def cap_text(text):\n",
    "    return text.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hi People I Love Teaching And Letsupgrade Too'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cap_text(\"hi people i love teaching and letsupgrade too\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing cap.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile cap.py\n",
    "def cap_text(text):\n",
    "    return text.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting test_cap.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile test_cap.py\n",
    "\n",
    "import unittest\n",
    "import cap\n",
    "\n",
    "class TestCapital(unittest.TestCase):\n",
    "    def testsingleword(self):\n",
    "        some_text = \"letsupgrade\"\n",
    "        result = cap.cap_text(some_text)\n",
    "        self.assertEqual(result,\"Letsupgrade\")\n",
    "    def testMultipleWord(self):\n",
    "        some_text = \"sorry for the buffer\"\n",
    "        result = cap.cap_text(some_text)\n",
    "        self.assertEqual(result,\"Sorry For The Buffer\")\n",
    "    def testTenWords(self):\n",
    "        some_text = \"i welcome you all to letsupgrade, a freedom providing startup in education\"\n",
    "        result = cap.cap_text(some_text)\n",
    "        self.assertEqual(result,\"I Welcome You All To Letsupgrade, A Freedom Providing Startup In Education\")\n",
    "    \n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    unittest.main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "...\r\n",
      "----------------------------------------------------------------------\r\n",
      "Ran 3 tests in 0.000s\r\n",
      "\r\n",
      "OK\r\n"
     ]
    }
   ],
   "source": [
    "! python test_cap.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
